<?php



namespace common\helps;
use Yii;


class api_jd
{
    public static function post($action, $data=null)
    {
        $path = '/var/log/lfl.log';
        elog('------------------------------------------------------',$path);
        elog($action,$path);

        $data = $data ? json_encode($data, JSON_UNESCAPED_UNICODE) : null;
        elog($data,$path);

        $headers = array('Content-Type: application/json; charset=utf-8');

        $url = Yii::$app->params['API_JD_PATH'] . $action;
        //$url = 'http://127.0.0.1:9012/test.php';
        $postUrl = $url;
        $ResData = null;
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL, $postUrl);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $ResData = curl_exec($ch);//运行curl
        curl_close($ch);
        elog($ResData,$path);
        if ($ResData) {
            $ResData = json_decode($ResData, 1);
        } else {
            $ResData = array('errCode' => 0, 'errMsg' => '失败');
        }

        return $ResData;
    }
}